//
//  WSSureOrderViewController.m
//  FD
//
//  Created by Yasin on 2018/10/26.
//  Copyright © 2018 ZhiYuan Network. All rights reserved.
//  确认订单: 入口：首页--外卖点餐0---->底部去结算

#define kZTLastTimeInputName @"kZTLastTimeInputName"
#define kZTLastTimeInputPhone @"kZTLastTimeInputPhone"



#import "WSSureOrderViewController.h"
//view
#import "WSSureOrderFooterView.h" //footerview
#import "WSHeaderView.h" //11-22 外送表头
#import "WSMeiYouDianJiHeaderView.h"//2019-2-18 新需求

#import "ZTHeaderView.h" //11-22 自提表头
#import "ZTHeaderViewNoAddress.h" //2019-1-12 纯展示

#import "WSTopEdgeView.h"//11-22 帽子🎩
#import "FDSureOrderBottomViewXin.h" //购物车底部view 重写
#import "WSSureOrderSectionHeader.h" //区头
#import "NewWSSureOrderCell.h" //cell


//model
#import "CanPinMsgModel.h" //11-21 废弃
#import "WSSureOrderModel.h" //外送订单模型
#import "MyAddressModel.h" //地址model
#import "ZTModel.h" //自提model
#import "AvaliableCouponModel.h" //可用的优惠券

//viewcontroller 可用优惠券
#import "AvaliableCouponViewController.h"
#import "NewWSPayTypeController.h"

#import "SendMealAddressAndTimeController.h" //送餐地址和时间
#import "RangeNotSupportViewController.h" //超出配送范围
#import "ZTViewController.h" // 自提选择餐厅
#import "FDTabBarController.h" /**下单成功之后跳转到订单页面*/
#import "WSPayUrlViewController.h" //支付链接控制器

#import "YingYeShiJianKeFuDianHuaHelper.h" //营业时间
#import "HomeShopCartHelper.h" //统计总金额+获取运费
#import "WSSureOrderControllerHelper.h" //工具(网络请求)
#import "WSSureOrderTableViewProxy.h"   //tv代理方法



@interface WSSureOrderViewController ()
<AvaliableCouponVCDelegate, //优惠券列表
WSSureOrderControllerHelperDelegate //负责网络请求
>

{
    /**6.用户选择的收货地址模型*/
    MyAddressModel *_receiveAddresModel;
    NSNumber *_deliveryType; /**送餐方式 0外送(默认 )； 1自提  WSTopEdgeView 需要配合显示效果*/
    //2019-2-15不要默认的了
    
    
    
    NSArray *_userCartItList; /**用户购物车id列表,已经来就组装*/
    AvaliableCouponModel *_couponModel; /**可用的优惠券*/
    ZTModel *_ztShopModel; /**自提返回门店  数据模型*/
    
    //记录订单总价
    CGFloat _actualSumPrice;
    
    /**2019-2-19记录运费*/
    CGFloat _tempFreight;
    
}


@property (nonatomic,  strong) UITableView *tableView;
/**1.餐品信息数据*/
@property (nonatomic,  strong) NSMutableArray *canPinMsgArrM;
/**3-1 外送表头*/
@property (nonatomic, strong) WSHeaderView *wsHeaderView;
/**2019-2-18新需求*/
@property (nonatomic, strong) WSMeiYouDianJiHeaderView *wsMYDJHeaderView;

/**3-2 自提表头*/
@property (nonatomic, strong) ZTHeaderView *ztHeaderView;
/**2019-1-12*/
@property (nonatomic, strong) ZTHeaderViewNoAddress *ztHeadeViewNoAddress;

/**3-3 帽子🎩*/
@property (nonatomic, strong) WSTopEdgeView *topEdgeView;
/**4.表尾*/
@property (nonatomic, strong) WSSureOrderFooterView *footerView;
/**5.底部bottomView*/
@property (nonatomic, strong) FDSureOrderBottomViewXin *bottomView;

/**5.有用信息model'*/
@property (nonatomic, strong) WSSureOrderModel *usefulModel;

/**11-22记录支付方式*/
@property (nonatomic, strong) NSNumber * wsPayType;


//11-22 控制器瘦身 修改
/**负责tableView的代理方法*/
@property (nonatomic, strong) WSSureOrderTableViewProxy *tvProxy;
/**负责网络请求*/
@property (nonatomic, strong) WSSureOrderControllerHelper *helper;

/**菊花*/
@property (nonatomic, strong) MBProgressHUD *hud;

/**1-10可用优惠券列表数组*/
@property (nonatomic, strong) NSArray *userCouponListArr;

@end

@implementation WSSureOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    _deliveryType = @(0); //默认是外送 //2019-2-15不要默认
    
    [self setupUI]; //页面
    [self helperClassDoSomeThing]; //工具类要做的事情
    
    [self.helper loadSureOrderData]; //请求数据
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
        //2019-2-19保存上次输入的姓名和联系方式
    self.ztHeaderView.nameTf.text = [[NSUserDefaults standardUserDefaults] objectForKey:kZTLastTimeInputName];
    self.ztHeaderView.phoneTF.text = [[NSUserDefaults standardUserDefaults] objectForKey:kZTLastTimeInputPhone];
}


- (void)setupUI {
    self.view.backgroundColor = kMainLightGrayColor; //灰色背景
    self.navigationItem.title = NSLocalizedString(Order_SureOrder, @"确认订单");
    
//    CGFloat y = FD_NavH; //12-31 真机显示会出问题
    CGFloat tv_h = SCREEN_HEIGHT - FD_TabbarSafeBottomMargin - 50;
    self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, tv_h);
    [self.view addSubview:self.tableView];
    
    //添加底部view
    self.bottomView.frame = CGRectMake(0, self.tableView.rb_bottom, SCREEN_WIDTH, 50);
    [self.view addSubview:self.bottomView];
    
    
    //表头表尾
    self.tableView.tableFooterView = self.footerView;
    
    
    self.tableView.contentInset = UIEdgeInsetsMake(60, 0, 0, 0); //上面出来一部分
//    self.tableView.tableHeaderView = self.headerView;

    self.tableView.tableHeaderView = self.wsMYDJHeaderView; //2019-2-18 新需求
//    self.tableView.tableHeaderView = self.wsHeaderView; //默认表
    [self.tableView addSubview:self.topEdgeView]; // CGRectMake(0, -50, SCREEN_WIDTH, 50);
}



/**工具类要做到事情 */
- (void)helperClassDoSomeThing {
    //    FDWeakSelf; 不是所有的都会造成循环应用
    //获取营业时间
    [YingYeShiJianKeFuDianHuaHelper obtainYingYeShiJianComplectionBlock:^(NSString * _Nonnull active, NSString * _Nonnull quiet) {
        [self.bottomView updateYingYeShiJianBegin:active end:quiet];
    }];
    
    [HomeShopCartHelper shopcartHelperTotalPriceBlock:^(CGFloat totalPrice) {
//        [self.bottomView updateshopcartBottomViewTotalPrice:totalPrice];
    }]; //更新总价
    
    
    //11-27 获取运费价格 运费价格计算接口请求比较慢，客户端处理为：如果接口请求中，运费显示“计算中...”，同时确认订单按钮为灰色不可点击状态，接口请求完毕后恢复正常
}


#pragma mark - getter
- (UITableView *)tableView {
    if (!_tableView) {
        //取消粘连效果
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStyleGrouped)];
        _tableView.backgroundColor = kMainLightGrayColor; //背景颜色
        //设置代理
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate = self.tvProxy;
        _tableView.dataSource = self.tvProxy;
        
        //注册
//        [_tableView registerNib:[UINib nibWithNibName:@"NewWSSureOrderCell" bundle:nil] forCellReuseIdentifier:@"NewWSSureOrderCell"];
        [_tableView registerClass:[NewWSSureOrderCell class] forCellReuseIdentifier:@"NewWSSureOrderCell"];
        [_tableView registerClass:[WSSureOrderSectionHeader class] forHeaderFooterViewReuseIdentifier:@"WSSureOrderSectionHeader"];
        
        _tableView.estimatedRowHeight = 44;
        _tableView.rowHeight = UITableViewAutomaticDimension;

        //取消多余的cell
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (NSMutableArray *)canPinMsgArrM {
    if (!_canPinMsgArrM) {
        _canPinMsgArrM = [NSMutableArray array];
    }
    return _canPinMsgArrM;
}

- (FDSureOrderBottomViewXin *)bottomView {
    if (!_bottomView) {
        CGFloat y = SCREEN_HEIGHT - FD_NavH - FD_TabbarSafeBottomMargin - 50;
        _bottomView = [[FDSureOrderBottomViewXin alloc] initWithFrame:CGRectMake(0, y, SCREEN_WIDTH, 50)];
        [_bottomView updateBtnBgcEnable:NO]; //没有拿到运费就一直不让点。
        
        FDWeakSelf;
        //block回調
        _bottomView.clickJieSuanBlock = ^{
            [weakSelf clickJieSunaAction];
        };
    }
    return _bottomView;
}

//!!!!:外送 自提点击事件 请求运费回调 ✅✅✅
- (WSMeiYouDianJiHeaderView *)wsMYDJHeaderView {
    if (!_wsMYDJHeaderView) {
        _wsMYDJHeaderView = [[WSMeiYouDianJiHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    }
    return _wsMYDJHeaderView;
}


/**外送表头*/
- (WSHeaderView *)wsHeaderView {
    if (!_wsHeaderView) {
        _wsHeaderView = [[WSHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 110)];
        FDWeakSelf;
        
        _wsHeaderView.wsHeaderViewSelectAddressBlock = ^{
            
//            self->_deliveryType = @(0); //默认外送 0 //2019-2-15 不要默认选中
            
            
//            送餐地址时间管理
            SendMealAddressAndTimeController *scdzsjVC = [[SendMealAddressAndTimeController alloc] init];
            
            //接收选择的收货地址
            scdzsjVC.addressBlock = ^(MyAddressModel * _Nonnull addressModel) {
                //成员变量，全局变量
                self->_receiveAddresModel = addressModel;
                
                //更新收货地址
                NSString *address = [NSString stringWithFormat:@"%@",addressModel.detail];
                
                NSString *manStr = NSLocalizedString(Home_Man, @"先生");
                NSString *womanStr = NSLocalizedString(Home_Woman, @"女士");
                
                NSString *sex = [addressModel.sex isEqualToNumber:@(0)] ? manStr :womanStr;
                NSString *nameSexPhoneStr = [NSString stringWithFormat:@"%@ %@ %@",addressModel.consignee,sex,addressModel.phone];
                NSString *resultStr = [NSString stringWithFormat:@"%@\n%@",address,nameSexPhoneStr];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    weakSelf.wsHeaderView.addressLb.text = resultStr;
                    
                    //显示右箭头
                    weakSelf.wsHeaderView.rightArrowIv.hidden = NO;
                });
                
                //11-27请求运费
                [weakSelf loadFreightWithAddressId:addressModel.addressId];
            };
            
            [weakSelf.navigationController pushViewController:scdzsjVC animated:YES];
        };
    }
    return _wsHeaderView;
}
//!!!!:请求运费 收货地址 运费价格计算接口请求比较慢，客户端处理为：如果接口请求中，运费显示“计算中...”，同时确认订单按钮为灰色不可点击状态，接口请求完毕后恢复正常
- (void)loadFreightWithAddressId:(NSNumber *)addressId {
    
    [HomeShopCartHelper shopcartHelperFreightWithUserShippingAddressId:addressId Block:^(CGFloat freight) {
        [self.bottomView updateFreightAndUpdateJieSuanBtnStateWithFreight:freight];
        
        _tempFreight = freight; /**2019-2-19记录运费*/
    }];
}

- (ZTHeaderView *)ztHeaderView {
    if (!_ztHeaderView) {
        _ztHeaderView = [[ZTHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 273)];
        
        //12-3和android保持一致:点击自提按钮跳转,点击地址不在跳转
        FDWeakSelf;
        _ztHeaderView.ztheaderViewBlock = ^{
// 每个方法内部会有两个默认参数 : self , _cmd
// Block implicitly(隐式) retains 'self'; explicitly(显示) mention 'self' to indicate this is intended behavior
//选择送餐方式block回调
            self->_deliveryType = @(1); //自提
            //跳转到自提页面
            ZTViewController *ztVC = [[ZTViewController alloc] init];
            [weakSelf.navigationController pushViewController:ztVC animated:YES];

            ztVC.ztVCBlock = ^(ZTModel * _Nonnull ztmodel) {
                FDLog(@"===========:%@",ztmodel);
                self->_ztShopModel = ztmodel;
                weakSelf.ztHeaderView.shopNameLb.text = ztmodel.name;
                weakSelf.ztHeaderView.addressLb.text = ztmodel.address;
            };
        };
    }
    return _ztHeaderView;
}

- (ZTHeaderViewNoAddress *)ztHeadeViewNoAddress {
    if (!_ztHeadeViewNoAddress) {
        _ztHeadeViewNoAddress = [[ZTHeaderViewNoAddress alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 212)];
    }
    return _ztHeadeViewNoAddress;
}


- (WSTopEdgeView *)topEdgeView {
    if (!_topEdgeView) {
        _topEdgeView = [[WSTopEdgeView alloc] initWithFrame:CGRectMake(0, -60, SCREEN_WIDTH, 60)];
        
        FDWeakSelf
        //切换表头
        _topEdgeView.clickWaiSongBtnBlcok = ^{
            weakSelf.tableView.tableHeaderView = weakSelf.wsHeaderView;
            self->_deliveryType = @(0); //外送
        };
        _topEdgeView.clickZiTiBtnBlock = ^{
            //2019-1-12
            weakSelf.tableView.tableHeaderView = weakSelf.ztHeadeViewNoAddress;
            self->_deliveryType = @(1);  //自提
            [weakSelf.bottomView updateBtnBgcEnable:YES]; //按钮可以点击
    
            //12-3 跳转外送点餐列表页面
            ZTViewController *ztVC = [[ZTViewController alloc] init];
            ztVC.ztVCBlock = ^(ZTModel * _Nonnull ztmodel) {
                FDLog(@"自提门店：%@",ztmodel);
                //2019-1-12有数的时候就切换表头
                weakSelf.tableView.tableHeaderView = weakSelf.ztHeaderView;

                self->_ztShopModel = ztmodel;
                weakSelf.ztHeaderView.shopNameLb.text = ztmodel.name;
                weakSelf.ztHeaderView.addressLb.text = ztmodel.address;
            };
            [weakSelf.navigationController pushViewController:ztVC animated:YES];
        };
    }
    return _topEdgeView;
}


//!!!!:选择支付方式
- (WSSureOrderFooterView *)footerView {
    if (!_footerView) {
        _footerView = [[WSSureOrderFooterView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150 + 10)]; //10px 灰色线条
        
        //根据请求数据进行赋值
        //可用优惠券
        NSString *avaliableCouponStr = NSLocalizedString(Order_AvaliableCoupon, @"可用优惠券");
        
        NSString *cntStr = self.usefulModel.couponCount ? ([NSString stringWithFormat:@"%@",self.usefulModel.couponCount]) : @"0";
        _footerView.avaliableCouponLb.text = [NSString stringWithFormat:@" %@(%@)",avaliableCouponStr,cntStr];
        
        //点击事件
        FDWeakSelf;
        _footerView.clickAvaliableCouponBlock = ^{
            AvaliableCouponViewController *avaliableCouponVC = [[AvaliableCouponViewController alloc] init];
            //订单总额。
//            avaliableCouponVC.orderAmount = @(110.0);
            
            //1-10标记可用优惠券列表数组
            avaliableCouponVC.couponListArr = weakSelf.userCouponListArr;
            avaliableCouponVC.delegate = weakSelf;//设置代理AvaliableCouponVCDelegate
            
            if (weakSelf.userCouponListArr.count) {
                [weakSelf.navigationController pushViewController:avaliableCouponVC animated:YES];
            }else {
                [JRToast showWithText:NSLocalizedString(L_ZanWuKeYongYouHuiQuan, @"暂无可用优惠券")];
            }
        };
        
        //显示支付方式
        _footerView.clickOnLineBlock = ^{
            weakSelf.wsPayType = @(0); //在线支付
        };
        _footerView.clickOffLineBlock = ^{
            weakSelf.wsPayType = @(1); //线下支付
        };
    }
    return _footerView;
}


- (WSSureOrderTableViewProxy *)tvProxy {
    if (!_tvProxy) {
        _tvProxy = [[WSSureOrderTableViewProxy alloc] init];
    }
    return _tvProxy;
}
- (WSSureOrderControllerHelper *)helper {
    if (!_helper) {
        _helper = [[WSSureOrderControllerHelper alloc] init];
        _helper.delegate = self; //设置代理 f'ads
    }
    return _helper;
}


#pragma mark - AvaliableCouponVCDelegate
//!!!!:2019-1-15优惠券
- (void)didSelectedAvaliableCoupon:(AvaliableCouponModel *)couponModel {
//    FDLog(@"使用的优惠券:%@",couponModel);
    _couponModel = couponModel;
    
    //不能直接修改:_actualSumPrice的值
    //考虑这样一种情况:A --> B 、B-->A 、A-->B 就会出问题。_actualSumPrice的值一直在减少
    
    CGFloat tempActualSumPrice = _actualSumPrice;
    CGFloat actualCouponPrice = 0.0; //实际优惠金额是。
    //实际付款总
    FDLog(@"tempActualSumPrice:%f",tempActualSumPrice);
    
    //优惠券计算规则
    //有限制商品、无限制商品
    if (couponModel.ruleGoodsArr.count) { //有限制商品
        
        //限制商品总价
        CGFloat limiteGoodsPrice = 0;
        for (NewCanPinMsgBigModel *bigModel in self.tvProxy.canPinMsgArrM) {
            if ([couponModel.ruleGoodsArr containsObject:bigModel.goods[@"id"]]) {
                limiteGoodsPrice += bigModel.price;
            }
        }
        
        FDLog(@"限制商品总价是:%f",limiteGoodsPrice);
        //如果是现金优惠券
        if ([couponModel.moneyType isEqualToNumber:@0]) { //现金优惠券
            if (limiteGoodsPrice > [couponModel.money floatValue]) { //优惠金额不能超过商品总价
                limiteGoodsPrice = [couponModel.money floatValue];
            }
        }else {
            limiteGoodsPrice *= (1- [couponModel.money floatValue]);
        }
        
        FDLog(@"处理后的显示商品总价:%f",limiteGoodsPrice);
        
        actualCouponPrice = limiteGoodsPrice;
        
        tempActualSumPrice -= limiteGoodsPrice;
        
    }else { //无限制商品
    
        //实际付款总价
        if ([couponModel.moneyType isEqualToNumber:@0]) { //现金优惠券
            
            actualCouponPrice = [couponModel.money floatValue];
            tempActualSumPrice -= [couponModel.money floatValue];
        }else { //百分比优惠券
            if ([couponModel.money floatValue] > 1.0) {
                FDLog(@"出错了：%@ ",couponModel.money);
            }
            
            tempActualSumPrice *= [couponModel.money floatValue]; //打 n 折扣
            
            actualCouponPrice = tempActualSumPrice *(1- [couponModel.money floatValue]);
        }
    }
    
    
    //最终总支付金额
    FDLog(@"原价:%f 优惠后的tempActualSumPrice:%f",_actualSumPrice,tempActualSumPrice);

    //更新界面
    [self.footerView updateCouponPrice:actualCouponPrice];
    
    
    [self.bottomView updateshopcartBottomViewTotalPrice:tempActualSumPrice originPrice:0 freight:_tempFreight];
}



#pragma mark - 点击去结算按钮
//!!!!:确认订单点击事件
- (void)clickJieSunaAction {
    
    if ([NSString isEmptyString:[NSString stringWithFormat:@"%@",_deliveryType]]) {
        [JRToast showWithText:@"请选择配送方式"];
        return;
    }
    
    
    //1.外送  请选择送餐地址
    if (!_receiveAddresModel.addressId && [_deliveryType isEqualToNumber:@(0)]) {
        [JRToast showWithText:NSLocalizedString(Order_chooseAddress, @"请选择送餐地址")];

        return;
    }
    
    if (!_ztShopModel.shopId && [_deliveryType isEqualToNumber:@(1)]) {
        [JRToast showWithText:NSLocalizedString(ToastPleaseZiTiMenDian, @"请选择自提门店")];
        return;
    }
    
    // 1 自提需要输入名字和电话☎️
    if ([_deliveryType isEqualToNumber:@(1)]) {
        
        //名字
        NSString *name = self.ztHeaderView.nameTf.text;
        //电话
        NSString *phone = self.ztHeaderView.phoneTF.text;
        if ([NSString isEmptyString:name]) {
            
            [JRToast showWithText:NSLocalizedString(Home_put_your_contacts_name, @"请输入联系人姓名")];
            return;
        }
        
        if ([NSString isEmptyString:phone]) {
            [JRToast showWithText:NSLocalizedString(Home_inputPhone, @"请输入联系电话")];
            return;
        }
    }
    
    //传给服务器的是NSNumber类型
    if ([NSString isEmptyString:[NSString stringWithFormat:@"%@",self.wsPayType]]) {
        [JRToast showWithText:NSLocalizedString(Order_SelectPayType, @"请选择支付方式")];
        return;
    }
   
    
    if ([_deliveryType isEqualToNumber:@(1)]) {
        //2019-2-19保存上次输入的姓名和联系方式
        [[NSUserDefaults standardUserDefaults] setValue:self.ztHeaderView.nameTf.text  forKey:kZTLastTimeInputName];
        [[NSUserDefaults standardUserDefaults] setValue:self.ztHeaderView.phoneTF.text  forKey:kZTLastTimeInputPhone];
    }
    
    
    
    
    //组装请求参数
    NSDictionary *parDic = [self assembleSubmitOrderParamWithPayType:self.wsPayType];
    
    //提交订单接口
    [self requestSubmitOrderWithParDic:parDic];
}

#pragma mark -  WSSureOrderControllerHelperDelegate
//开始请求数据
- (void)beginRequestData {
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}


//数据请求成功回调
- (void)didRequestDataWithArr:(NSArray *)arr wsSureOrderModel:(WSSureOrderModel *)soModel userCouponList:(NSArray *)userCouponList {

    self.tvProxy.canPinMsgArrM = arr;
    
    //1-10标记可用优惠券列表数组
    self.userCouponListArr = userCouponList;

    [self.footerView updateAvaliableCouponCount:userCouponList.count];
    
    [self.tableView reloadData]; //刷新数据源
    [self.hud hideAnimated:YES];
}

//请求数据失败
- (void)didFailedRequestDataWithErrorMessage:(NSString *)errorMessage {
    [self.hud hideAnimated:YES];
}

//!!!!:计算总价
- (void)didCountingCurrentSumPrice:(CGFloat)currentSumPrice originSumPrice:(CGFloat)originsumPrice {
    
    
    [self.bottomView updateshopcartBottomViewTotalPrice:currentSumPrice originPrice:originsumPrice freight:_tempFreight];
    //记录订单总价
    _actualSumPrice = currentSumPrice;
}

//!!!!: 提交订单
- (void)requestSubmitOrderWithParDic:(NSDictionary *)parDic {
    
    [MBProgressHUD fd_showHUDToView:self.view]; //菊花
    
    [FDHttpAPi requestDDOrderSumitParDic:parDic successBlock:^(id responseObjc) {
        
        [MBProgressHUD fd_hideHUDFromView:self.view]; //移除菊花
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            //12-4判断是线上还是线下支付方式
            if ([self.wsPayType isEqualToNumber:@(0)]) { //如果是线上支付方式就直接跳转到支付页面
                NSString *orderId = responseObjc[@"data"][@"id"];
                [self skipToPayUrlVCWithOrderID:orderId];
                
            }else {
                //跳转到结算跳转到订单列表页面。
                [self skipToOrderVC];
            }
        }else if([event isEqualToString:RANGE_NOT_SUPPORTED]){ //超出配送范围
            [JRToast showWithText:responseObjc[describe]]; //异常
            //显示超出配送范围。
            RangeNotSupportViewController *notSupportVC = [[RangeNotSupportViewController alloc] init];
            notSupportVC.addressModel = self->_receiveAddresModel; //使用经纬度
            
            [self.navigationController pushViewController:notSupportVC animated:YES];
            
        }else {
            [JRToast showWithText:responseObjc[describe]]; //异常
            
            [MBProgressHUD fd_hideHUDFromView:self.view]; //移除菊花
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]); //异常
    }];
}

/**跳转到订单列表页面*/
- (void)skipToOrderVC{

//    [self.navigationController popViewControllerAnimated:NO]; //当前控制器无法销毁。
    [self.navigationController popToRootViewControllerAnimated:NO]; //销毁控制器
    
    FDTabBarController *tabVC = [FDTabBarController shareFDTabarVC];
    tabVC.selectedIndex = 2; //订单页面
    UIApplication *app = [UIApplication sharedApplication];
    app.keyWindow.rootViewController = tabVC;
}


/**组装提交订单参数 接口地址:/user/order/submit */
- (NSDictionary *)assembleSubmitOrderParamWithPayType:(NSNumber *)payType{
    
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"deliveryType"] = _deliveryType; //1.配送方式
    parDic[@"paymentType"] = payType; //2.支付方式
    parDic[@"userShippingAddressId"] = _receiveAddresModel.addressId;//3.收货地址编号
    
    //备注:可以为空，有就赋值 此字段必传 数据丢失
    //有就是自己,没有就是空
//    NSString *remark = _footerView.markTF.text ? : @"";
//    parDic[@"remark"] = remark; //5.

    
    //优惠券列表,可以为空，有就赋值，
    if (![NSString isEmptyString:[NSString stringWithFormat:@"%@",_couponModel.couponId]]) {
        parDic[@"userCouponId"] = _couponModel.couponId; //6.
    }
    
    
    
    //门店编号,shopId,如果是自提(1),必须传递。 暂未处理
    if ([_deliveryType isEqualToNumber:@(1)]) { //自提
        parDic[@"shopId"] = _ztShopModel.shopId;
        parDic[@"consignee"] = self.ztHeaderView.nameTf.text;
        parDic[@"phone"] = self.ztHeaderView.phoneTF.text;
    }
    
    FDLog(@"提交订单请求参数:%@",parDic);
    
    return [parDic copy];
}


#pragma mark - 12-4支付页面跳转
/**立即支付.获取订单支付链接*/
- (void)skipToPayUrlVCWithOrderID:(NSString *)orderId {
    
    [MBProgressHUD fd_showHUDToView:self.view];
    
    [FDHttpAPi requestUserOrderPaymentUrlWithOrderId:orderId successBlock:^(id responseObjc) {
        
        [MBProgressHUD fd_hideHUDFromView:self.view];
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            FDLog(@"%@",responseObjc[@"data"][@"url"]);
            
            WSPayUrlViewController *payurlVC = [[WSPayUrlViewController alloc] init];
            payurlVC.payUrl = responseObjc[@"data"][@"url"];
            [self.navigationController pushViewController:payurlVC animated:YES];
        }
    } fialBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
        
        [MBProgressHUD fd_hideHUDFromView:self.view];
    }];
}


- (void)dealloc {
    FDLog(@"被销毁--%s",__FUNCTION__);
}
@end

